package com.msb.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.msb.common.vo.CommonPageReq;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.entity.product.AttrGroupEntity;
import com.msb.mall.core.vo.product.AttrGroupRelationVo;
import com.msb.mall.product.service.AttrAttrgroupRelationService;
import com.msb.mall.product.service.AttrService;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.product.service.AttrGroupService;
import com.msb.common.utils.PageUtils;



/**
 * 属性分组
 *
 * @author leixu
 * @email xuleiwolf@163.com
 * @date 2024-04-19 20:08:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService relationService;
    @Autowired
    private AttrService attrService;

    /**
     * 分页查询
     */
    @PostMapping("/page/{catelogId}")
    public CommonResult page(@RequestBody CommonPageReq<Map<String, Object>> req, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(req, catelogId);

        return CommonResult.success(null, page);
    }

    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    public CommonResult list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPageOld(params, catelogId);

        return CommonResult.success(null, page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public CommonResult info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		// 根据找到的属性组对应的分类id然后找到对应的[一级、二级、三级]数据
        Long catelogId = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(paths);
        return CommonResult.success(attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public CommonResult save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return CommonResult.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public CommonResult update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public CommonResult delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return CommonResult.success();
    }

    @PostMapping("/attr/relation")
    public CommonResult saveBatch(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.saveBatch(vos);
        return CommonResult.success();
    }

    @PostMapping("/attr/relation/delete")
    public CommonResult relationDelete(@RequestBody List<AttrGroupRelationVo> vos) {
        attrService.deleteRelation(vos);
        return CommonResult.success();
    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public CommonResult attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        return CommonResult.success(attrService.getRelationAttr(attrGroupId));
    }

    @PostMapping("/{attrGroupId}/noattr/relation")
    public CommonResult attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                                       @RequestBody CommonPageReq<Map<String, Object>> req) {
        return CommonResult.success(attrService.getNoAttrRelation(req, attrGroupId));
    }

    @GetMapping("/{catelogId}/withattr")
    public CommonResult getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 根据三级分类的编号获取对应的属性组和属性组的属性信息
        return CommonResult.success(attrGroupService.getAttrgroupWithAttrsByCatelogId(catelogId));
    }
}
